package com.ywm.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ywm.commonUtil.R;
import com.ywm.eduService.entity.EduSubject;
import com.ywm.eduService.entity.EduTeacher;
import com.ywm.eduService.entity.subject.Subject;
import com.ywm.eduService.service.EduSubjectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-17
 */
@RestController
@RequestMapping("/eduService/edu-subject")
public class EduSubjectController {
    @Autowired
    private EduSubjectService subjectService;

    //获取上传的文件，将内容读取出来
    @ApiOperation(value = "添加课程分类")
    @PostMapping("addSubject")
    public R addSubject(MultipartFile file) {
        //上传过来的文件
        subjectService.saveSubject(file, subjectService);
        return R.ok();
    }

    // 返回课程分类列表
    @ApiOperation(value = "返回课程分类列表")
    @GetMapping("/getAllList")
    public R getAllList() {
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
        QueryWrapper<EduSubject> wrapper2=null;
        wrapper.eq("parent_id",0);
        //存储一级列表
        List<EduSubject> list1 = subjectService.list(wrapper);
        List<Subject> l1=new ArrayList<>();
        for (EduSubject eduSubject : list1) {
            List<Subject> l2=new ArrayList<>();
            Subject subject1 = new Subject(eduSubject.getId(), eduSubject.getTitle(), null);
            wrapper2=new QueryWrapper<>();
            wrapper2.eq("parent_id",eduSubject.getId());
            //存储二级列表
            List<EduSubject> list2 = subjectService.list(wrapper2);
            for (EduSubject subject : list2) {
                l2.add(new Subject(subject.getId(),subject.getTitle(),null));
            }
            subject1.setChildren(l2);
            l1.add(subject1);
        }
        return R.ok().data("list",l1);
    }
}

